// 相机权限工具
class CameraHelper {
  // 检查相机权限
  static checkCameraAuth() {
    return new Promise((resolve) => {
      wx.getSetting({
        success: (res) => {
          const authSetting = res.authSetting;
          if (authSetting["scope.camera"] === true) {
            resolve("authorized");
          } else if (authSetting["scope.camera"] === false) {
            resolve("denied");
          } else {
            resolve("notSet");
          }
        },
        fail: () => {
          resolve("error");
        },
      });
    });
  }

  // 请求相机权限
  static requestCameraAuth() {
    return new Promise((resolve) => {
      wx.authorize({
        scope: "scope.camera",
        success: () => {
          resolve(true);
        },
        fail: () => {
          resolve(false);
        },
      });
    });
  }

  // 选择图片
  static async chooseImage(options = {}) {
    try {
      // 检查权限状态
      const authStatus = await this.checkCameraAuth();

      if (authStatus === "denied") {
        // 用户拒绝了权限，引导去设置
        const confirmed = await this.showAuthModal();
        if (confirmed) {
          const settingRes = await this.openSetting();
          if (settingRes.authSetting["scope.camera"]) {
            return await this.openImagePicker(options);
          }
        }
        return null;
      } else if (authStatus === "notSet") {
        // 首次请求权限
        const granted = await this.requestCameraAuth();
        if (granted) {
          return await this.openImagePicker(options);
        }
        return null;
      } else if (authStatus === "authorized") {
        // 已有权限，直接选择图片
        return await this.openImagePicker(options);
      }

      return null;
    } catch (error) {
      console.error("图片选择失败:", error);
      wx.showToast({
        title: "图片选择失败",
        icon: "none",
      });
      return null;
    }
  }

  // 打开图片选择器
  static openImagePicker(options) {
    return new Promise((resolve) => {
      const defaultOptions = {
        count: 9,
        sizeType: ["compressed"],
        sourceType: ["album", "camera"],
      };

      wx.chooseImage({
        ...defaultOptions,
        ...options,
        success: (res) => {
          resolve(res.tempFilePaths);
        },
        fail: (err) => {
          console.error("图片选择器打开失败:", err);
          wx.showToast({
            title: "图片选择失败",
            icon: "none",
          });
          resolve(null);
        },
      });
    });
  }

  // 显示权限说明弹窗
  static showAuthModal() {
    return new Promise((resolve) => {
      wx.showModal({
        title: "需要相机权限",
        content: "为了拍摄照片，需要获取相机权限",
        confirmText: "去设置",
        cancelText: "取消",
        success: (res) => {
          resolve(res.confirm);
        },
      });
    });
  }

  // 打开设置页面
  static openSetting() {
    return new Promise((resolve) => {
      wx.openSetting({
        success: resolve,
        fail: () => {
          resolve({ authSetting: {} });
        },
      });
    });
  }
}

export default CameraHelper;
